package com.example.myapplicationktkt2.ui

import android.os.Bundle
import android.widget.ListView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.myapplicationktkt2.R
import com.example.myapplicationktkt2.model.Person
import com.example.myapplicationktkt2.adapter.PersonListAdapter

/**
 * ComplexListViewDemoActivity演示了如何在ListView中显示包含图像和信息的复杂对象
 *
 * @author Example
 * @since 1.0
 */
class ComplexListViewDemoActivity : AppCompatActivity() {

    // UI组件声明
    private lateinit var listView: ListView

    // 数据源
    private lateinit var personList: List<Person>

    // 适配器
    private lateinit var personListAdapter: PersonListAdapter

    /**
     * Activity创建时调用的方法
     *
     * @param savedInstanceState 保存的实例状态
     */
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_complex_listview_demo)

        initData()
        initViews()
        setupListeners()
    }

    /**
     * 初始化数据
     */
    private fun initData() {
        personList = listOf(
            Person(
                1,
                "张三",
                25,
                "zhangsan@example.com",
                R.drawable.ic_launcher_foreground
            ),
            Person(
                2,
                "李四",
                30,
                "lisi@example.com",
                R.drawable.ic_launcher_foreground
            ),
            Person(
                3,
                "王五",
                28,
                "wangwu@example.com",
                R.drawable.ic_launcher_foreground
            ),
            Person(
                4,
                "赵六",
                35,
                "zhaoliu@example.com",
                R.drawable.ic_launcher_foreground
            ),
            Person(
                5,
                "钱七",
                22,
                "qianqi@example.com",
                R.drawable.ic_launcher_foreground
            ),
            Person(
                6,
                "孙八",
                27,
                "sunba@example.com",
                R.drawable.ic_launcher_foreground
            )
             ,
            Person(
                7,
                "周九",
                29,
                "zhoujiu@example.com",
                R.drawable.ic_launcher_foreground
            )
                ,
            Person(
                8,
                "吴九",
                26,
                "wudjiu@example.com",
                R.drawable.ic_launcher_foreground
            ),
            Person(
                9,
                "郑十",
                31,
                "zhengshi@example.com",
                R.drawable.ic_launcher_foreground
            ),
            Person(
                10,
                "王十",
                33,
                "wangshi@example.com",
                R.drawable.ic_launcher_foreground
            )
        )
    }

    /**
     * 初始化视图组件
     */
    private fun initViews() {
        listView = findViewById(R.id.listView)

        // 创建适配器
        personListAdapter = PersonListAdapter(this, personList)

        // 设置适配器
        listView.adapter = personListAdapter

        // 设置列表项点击监听器
        listView.setOnItemClickListener { _, _, position, _ ->
            val person = personList[position]
            Toast.makeText(
                this,
                "点击了: ${person.name}, 年龄: ${person.age}",
                Toast.LENGTH_SHORT
            ).show()
        }

        // 设置列表项长按监听器
        listView.setOnItemLongClickListener { _, _, position, _ ->
            val person = personList[position]
            Toast.makeText(
                this,
                "长按了: ${person.name}, 邮箱: ${person.email}",
                Toast.LENGTH_SHORT
            ).show()
            true // 消费该事件
        }
    }

    /**
     * 设置监听器
     */
    private fun setupListeners() {
        // 这里可以添加其他监听器
    }
}